#include <iostream> //对应 C里的 stdio.h
#include <string>   //C ++ string 类头文件
#include <cstring>  //string.h  C 字符串相关函数头文件

template<typename T>
class MyArray {
private:
    T* m_data;         // 指向动态分配数组的指针
    size_t m_size;     // 当前元素数量
    size_t m_capacity; // 当前容量

    // 扩容函数
    void resize(size_t newCapacity) {
        T* newData = new T[newCapacity];
        for (size_t i = 0; i < m_size; ++i) {
            newData[i] = m_data[i];
        }
        delete[] m_data;
        m_data = newData;
        m_capacity = newCapacity;
    }

public:
    // 默认构造函数
    MyArray() : m_data(nullptr), m_size(0), m_capacity(0) {
        resize(10); // 初始容量设为10
    }

    // 带初始大小的构造函数
    explicit MyArray(size_t size) : m_data(nullptr), m_size(size), m_capacity(size) {
        m_data = new T[m_capacity];
    }

    // 析构函数
    ~MyArray() {
        delete[] m_data;
    }

    // 拷贝构造函数
    MyArray(const MyArray& other) : m_size(other.m_size), m_capacity(other.m_capacity) {
        m_data = new T[m_capacity];
        for (size_t i = 0; i < m_size; ++i) {
            m_data[i] = other.m_data[i];
        }
    }

    // 移动构造函数
    MyArray(MyArray&& other) noexcept : m_data(other.m_data), m_size(other.m_size), m_capacity(other.m_capacity) {
        other.m_data = nullptr;
        other.m_size = 0;
        other.m_capacity = 0;
    }

    // 拷贝赋值运算符
    MyArray& operator=(const MyArray& other) {
        if (this != &other) {
            delete[] m_data;
            m_size = other.m_size;
            m_capacity = other.m_capacity;
            m_data = new T[m_capacity];
            for (size_t i = 0; i < m_size; ++i) {
                m_data[i] = other.m_data[i];
            }
        }
        return *this;
    }

    // 移动赋值运算符
    MyArray& operator=(MyArray&& other) noexcept {
        if (this != &other) {
            delete[] m_data;
            m_data = other.m_data;
            m_size = other.m_size;
            m_capacity = other.m_capacity;
            other.m_data = nullptr;
            other.m_size = 0;
            other.m_capacity = 0;
        }
        return *this;
    }

    // 获取元素数量
    size_t size() const {
        return m_size;
    }

    // 获取容量
    size_t capacity() const {
        return m_capacity;
    }

    // 判断数组是否为空
    bool empty() const {
        return m_size == 0;
    }

    // 在末尾添加元素
    void push_back(const T& value) {
        if (m_size >= m_capacity) {
            resize(m_capacity * 2);
        }
        m_data[m_size++] = value;
    }

    // 移除末尾元素
    void pop_back() {
        if (m_size > 0) {
            --m_size;
        }
    }

    // 在指定位置插入元素
    void insert(size_t index, const T& value) {
        if (index > m_size) {
            throw std::out_of_range("Index out of range");
        }

        if (m_size >= m_capacity) {
            resize(m_capacity * 2);
        }

        // 移动元素为新元素腾出位置
        for (size_t i = m_size; i > index; --i) {
            m_data[i] = m_data[i - 1];
        }

        m_data[index] = value;
        ++m_size;
    }

    // 移除指定位置的元素
    void erase(size_t index) {
        if (index >= m_size) {
            throw std::out_of_range("Index out of range");
        }

        // 移动元素覆盖要删除的元素
        for (size_t i = index; i < m_size - 1; ++i) {
            m_data[i] = m_data[i + 1];
        }

        --m_size;
    }

    // 重载[]运算符
    T& operator[](size_t index) {
        return m_data[index];
    }

    const T& operator[](size_t index) const {
        return m_data[index];
    }

    // 安全的元素访问
    T& at(size_t index) {
        if (index >= m_size) {
            throw std::out_of_range("Index out of range");
        }
        return m_data[index];
    }

    const T& at(size_t index) const {
        if (index >= m_size) {
            throw std::out_of_range("Index out of range");
        }
        return m_data[index];
    }

    // 获取第一个元素
    T& front() {
        return m_data[0];
    }

    const T& front() const {
        return m_data[0];
    }

    // 获取最后一个元素
    T& back() {
        return m_data[m_size - 1];
    }

    const T& back() const {
        return m_data[m_size - 1];
    }

    // 清空数组
    void clear() {
        m_size = 0;
    }

    // 重载输出运算符
    friend std::ostream& operator<<(std::ostream& os, const MyArray& arr) {
        os << "[";
        for (size_t i = 0; i < arr.m_size; ++i) {
            os << arr.m_data[i];
            if (i < arr.m_size - 1) {
                os << ", ";
            }
        }
        os << "]";
        return os;
    }
};
